﻿package com.tvein.sanguo.swc.action.role
{
	//flash
	import flash.display.MovieClip;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.KeyboardEvent;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import fl.controls.TextInput;
	//A3D
	//custom
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	public class RoleDelete extends MovieClip
	{
		public static const NAME:String = "Role-Delete";

		private var sourceServer:String;
		private var effectVol:int = 100;
		private var roleID:int;
		
		private var txtRemove:TextInput;
		
		public function RoleDelete()
		{
			stage?init(null):addEventListener(Event.ADDED_TO_STAGE, init);
		}

		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.addEventListener(KeyboardEvent.KEY_UP, _keyDownHandler);
			stage.addEventListener(Event.RESIZE, _resizeUI);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			txtRemove = new TextInput();
			txtRemove.x = 180;
			txtRemove.y = 220;
			txtRemove.width = 190;
			txtRemove.height = 22;
			this.addChild(txtRemove);
			
			txtName.selectable = txtStyleName.selectable = txtInfo.selectable = false;
			txtName.mouseEnabled = txtStyleName.mouseEnabled = txtInfo.mouseEnabled = false;
					
			btnConfirm.visible = false;
			
			btnBack.addEventListener(MouseEvent.CLICK, onClick);
			btnConfirm.addEventListener(MouseEvent.CLICK, onClick);
			
			_resizeUI();
		}
	
		public function console(Type:String, params:Object):void
		{
			switch (Type)
			{
				case "setSource":
					sourceServer = params.Address;
					break;
				case "setEffect":
					effectVol = params.Volume;
					break;
				case "setData":
					setData(params);
					break;
			}
		}

		private function setData(params:Object):void
		{
			var StaminaNum:int;
			var EnergyNum:int;
			var ForceNum:int;
			var IntelligenceNum:int;
			var CommandNum:int;
			var PoliticsNum:int;
			
			for (var key:String in params)
			{
				switch(key)
				{
					case "Role_ID":
						roleID = int(params[key]);
						break;
					case "RoleName":
						txtName.text = params[key];
						break;
					case "StyleName":
						txtStyleName.text = params[key];
						break;
					case "Stamina":
						StaminaNum = params[key];
						break;
					case "Energy":
						EnergyNum = params[key];
						break;
					case "Force":
						ForceNum = params[key];
						break;
					case "Intelligence":
						IntelligenceNum = params[key];
						break;
					case "Command":
						CommandNum = params[key];
						break;
					case "Politics":
						PoliticsNum = params[key];
						break;
					case "ImageID":
						var faceloader:Loader = new Loader();
						faceloader.load(new URLRequest(sourceServer + "/SanGuo/role/face/" + params[key] + ".png"));
						faceloader.contentLoaderInfo.addEventListener(Event.COMPLETE, _onFaceLoad);
						break;
				}
			}
			
			txtInfo.htmlText = "体力：" + StaminaNum + " 精力：" + EnergyNum + "<br>";
			txtInfo.htmlText += "武力：" + ForceNum + " 智力：" + IntelligenceNum + "<br>";
			txtInfo.htmlText += "统率：" + CommandNum + " 政治：" + PoliticsNum + "";
		}
		
		private function _onFaceLoad(evt:Event):void
		{
			evt.target.removeEventListener(Event.COMPLETE, _onFaceLoad);
			
			evt.target.content.x = 54;
			evt.target.content.y = 93;
			
			this.addChild(evt.target.content);
		}
		
		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.removeEventListener(KeyboardEvent.KEY_UP, _keyDownHandler);
			stage.removeEventListener(Event.RESIZE, _resizeUI);
			
			btnBack.removeEventListener(MouseEvent.CLICK, onClick);
			btnConfirm.removeEventListener(MouseEvent.CLICK, onClick);
		}

		private function _keyDownHandler(evt:KeyboardEvent):void
		{
			btnConfirm.visible = (txtRemove.text == "REMOVE");
		}

		private function _resizeUI(evt:Event = null):void
		{			
			this.x = (stage.stageWidth - this.width) * .5
			if ((stage.stageHeight - this.height) > 100)
			{
				this.y = (stage.stageHeight - this.height) * .5;
			}
			else
			{
				this.y = 50;
			}
		}

		private function onClick(evt:MouseEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol * .01);
			
			switch(evt.target.name)
			{
				case "btnBack":
					dispatchEvent(new SwcEvent(NAME, ["-roleList"]));
					break;
				case "btnConfirm":
					dispatchEvent(new SwcEvent(NAME, ["-roleDelete", roleID]));
					break;
			}
		}
	}
}